/**
 * Created with IntelliJ IDEA.
 * User: Alexey Svitelskyy
 * Date Time: 11.04.13 13:10
 * Version: 1.2
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;


public class my_new_game {

    static final int MAX_OBJECTS = 21;
    static final int OBJECTS_TO_TAKE[] = {1, 2, 3, 4, 5};
    static final String YES = "Y";
    static final String NO = "N";
    static boolean playerMove;
    static boolean twoPlayersGame;

    public static void main(String[] args) throws IOException {
        twoPlayersGame = typeGame();
        playerMove = randBoolean();
        do {
            int takenObjects = newGame();
            do {
                int count = 0;
                printNextStepMessage();
                count = nextMove(takenObjects);
                print(String.format("Moved %s of %s change %s\n",
                        count, takenObjects, takenObjects - count));

                takenObjects -= count;
                playerMove = !playerMove;
            } while (takenObjects > 0);
            printWinMessage();
        } while (wantToPlayMore());

    }

    public static boolean typeGame() throws IOException {
        print("1. Player vs Computer\n");
        print("2. Player vs Player\n\n");
        print("Please, select type of game: \n");
        boolean result = ((getValidateInteger(1, 2) - 1) != 0);
        return result;
    }

    private static int newGame() {
        print(String.format("We have %s objects\n", MAX_OBJECTS));
        print("You can take in one move " + Arrays.toString(OBJECTS_TO_TAKE)
                + " objects\n");

        return MAX_OBJECTS;
    }

    private static int computerMove(int Sum) {
        if (Sum % (OBJECTS_TO_TAKE[0] + OBJECTS_TO_TAKE[4]) == 0) {
            return OBJECTS_TO_TAKE[0];
        } else {
            return Sum % (OBJECTS_TO_TAKE[0] + OBJECTS_TO_TAKE[4]);
        }
    }

    private static void printNextStepMessage() {
        if (twoPlayersGame || playerMove) {
            print(String.format("Player%s move now. Please make it:\n", playerMove ? 1 : 2));
        } else if (!twoPlayersGame && !playerMove) {
            print("Computer have move now. Move is:\n");
        }
    }

    private static int nextMove(int Sum) throws IOException {
        if (twoPlayersGame || playerMove) {
            return getValidateInteger(OBJECTS_TO_TAKE);
        } else if (!twoPlayersGame && !playerMove) {
            return computerMove(Sum);
        } else {
            return 0;
        }
    }

    private static void printWinMessage() {
        if (twoPlayersGame || !playerMove) {
            print(String.format("Player%s are winning.\n", playerMove ? 2 : 1));
        } else if (!twoPlayersGame && playerMove) {
            print("Computer winning.\n");
        }
    }

    public static boolean wantToPlayMore() throws IOException {
        print("Want to play more (Y/N): ");
        boolean result = getValidateBoolean();
        return result;
    }

    public static boolean getValidateBoolean() throws IOException {
        String getText = YES;
        do {
            getText = getString();
            if (!getText.equalsIgnoreCase(YES) && !getText.equalsIgnoreCase(NO))
                print("Not valid input. Try again: ");
        } while (!getText.equalsIgnoreCase(YES) && !getText.equalsIgnoreCase(NO));
        return getText.equalsIgnoreCase(YES);
    }

    public static int getValidateInteger(int... variants) throws IOException {
        String getText;
        boolean Ok = false;
        do {
            getText = getString();
            for (int index = 0; index < variants.length; index++) {
                if (Integer.valueOf(getText) == variants[index]) {
                    Ok = true;
                    continue;
                }
            }
            if (!Ok)
                print("Not valid input. Try again: ");
        } while (!Ok);
        return Integer.valueOf(getText);
    }

    private static boolean randBoolean() {
        Random random = new Random();
        return random.nextBoolean();
    }

    public static String getString() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        return bufferedReader.readLine();
    }

    private static void print(String text) {
        System.out.print(text);
    }

    private static int booleanToInteger(boolean b) {
        return b ? 1 : 0;
    }
}
